package fr.grobim.hangovers.gestionBudget.beans.factures;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import fr.grobim.hangovers.gestionBudget.beans.comptes.Membre;

@Entity
@Table(name = "facture")
public class Facture {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id")
	private Integer id;

	@ManyToOne
	@JoinColumn(name = "id_source", nullable = false)
	private SourceFacture source;

	@ManyToOne
	@JoinColumn(name = "id_payeur_par_defaut", nullable = true)
	private Membre payeurParDefaut;

	@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	@JoinTable(name = "factures_membres_concernes_par_defaut", joinColumns = { @JoinColumn(name = "id_facture")}, inverseJoinColumns = { @JoinColumn(name = "id_membre", nullable = false, updatable = false, insertable = false) })
	private Set<Membre> membresConcernesParDefaut;

	@Column(name = "periodicite", nullable = false, length = 50)
	private String periodicite;

	@Column(name = "montant_de_base", nullable = true)
	private Double montantDeBase;

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "facture")
	private Set<ReglementFacture> reglements;

	public Facture() {

	}

	public Facture(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public SourceFacture getSource() {
		return source;
	}

	public void setSource(SourceFacture source) {
		this.source = source;
	}

	public Membre getPayeurParDefaut() {
		return payeurParDefaut;
	}

	public void setPayeurParDefaut(Membre payeurParDefaut) {
		this.payeurParDefaut = payeurParDefaut;
	}

	public Set<Membre> getMembresConcernesParDefaut() {
		return membresConcernesParDefaut;
	}

	public void setMembresConcernesParDefaut(Set<Membre> membresConcernesParDefaut) {
		this.membresConcernesParDefaut = membresConcernesParDefaut;
	}

	public String getPeriodicite() {
		return periodicite;
	}

	public PeriodiciteFacture getPeriodiciteEnum() {
		return PeriodiciteFacture.get(this.periodicite);
	}

	public void setPeriodicite(String periodicite) {
		this.periodicite = periodicite;
	}

	public void setPeriodicite(PeriodiciteFacture periodicite) {
		if (periodicite != null) {
			this.periodicite = periodicite.getName();
		}
	}

	public Double getMontantDeBase() {
		return montantDeBase;
	}

	public void setMontantDeBase(Double montantDeBase) {
		this.montantDeBase = montantDeBase;
	}

	public Set<ReglementFacture> getReglements() {
		return reglements;
	}

	public void setReglements(Set<ReglementFacture> reglements) {
		this.reglements = reglements;
	}

}
